<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="search()">查询</el-button>
        <el-button v-if="isAuth('form:template:update')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('form:template:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column> 
      <el-table-column
        prop="code"
        header-align="center"
        align="center"
          width="90"
        label="表单编号">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="模板名称">
      </el-table-column> 
      <el-table-column
        prop="version"
        header-align="center"
        align="center"
        width="50"
        label="版本">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
          width="70"
        label="状态">
        <template slot-scope="scope">
            <span v-if="scope.row.status == 1">发布</span>
            <span v-else>暂存</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="createUserName"
        header-align="center"
        align="center"
          width="120"
        label="创建人">
        <template  slot-scope="scope">
          <user :value="scope.row.createId" />
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
          width="150"
        label="创建时间">
      </el-table-column> 
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" v-if="isAuth('form:template:update')" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
           <el-button type="text" size="small" v-if="isAuth('form:template:update') && scope.row.status == 0" @click="publish(scope.row.id)">发布</el-button>
            <el-button type="text" size="small" v-if="isAuth('form:template:update') && scope.row.version > 1 && scope.row.status == 0" @click="revoke(scope.row.id)">撤回</el-button>
          <el-button type="text" size="small" v-if="isAuth('form:template:delete')" @click="deleteHandle(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalCount"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './add-or-update'
  import * as templateApi from  '@/api/form/template'
  export default {
    data () {
      return {
        dataForm: {
          name: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalCount: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.dataForm.name =  this.$route.query.name;
      
      let pageIndex = this.$route.query.pageIndex; 
      if(pageIndex) {
        this.pageIndex = parseInt(pageIndex); 
      }

      let pageSize = this.$route.query.pageSize; 
      if(pageSize) {
        this.pageSize = parseInt(pageSize); 
      }
     
      this.getDataList();
    },
    methods: {
       refresh(){
        // this.$router.push({name:'item_formtemplate',query: {key:this.dataForm.key,pageIndex:this.pageIndex,pageSize:this.pageSize}});
      },
      search(){
         this.pageIndex = 1 ;
         this.getDataList();
         this.refresh();
      }, 
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true

        templateApi.getList({'page': this.pageIndex, 'limit': this.pageSize, 'name': this.dataForm.name})  
          .then(({data}) => {
            if (data && data.code === 0) {
              this.dataList = data.data.list
              this.totalCount = data.data.totalCount
              if(data.data.page != this.pageIndex) {
              	this.pageIndex = data.data.page
            	}
            } else {
              this.dataList = []
              this.totalCount = 0
              this.pageIndex = 1
            }
           
          }).finally(()=>{
          	this.dataListLoading = false 
          })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
        this.refresh()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
        this.refresh()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      revoke(id) {
        this.$confirm(`确定对当前模板版本进行撤销操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          templateApi.revoke(id).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1000
              })
              this.getDataList()
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      publish(id) {
        this.$confirm(`确定对当前模板进行发布操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          templateApi.publish(id).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1000
              })
              this.getDataList()
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 删除
      deleteHandle (row) {
        var ids = row ? [row.id] : this.dataListSelections.map(item => {
          return item.id
        })
        var names = row ? [row.name] : this.dataListSelections.map(item => {
          return item.name
        })
        this.$confirm(`确定对[名称=${names.join(',')}]得数据进行[${row ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
         templateApi.remove(ids).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1000
              })
              this.getDataList()
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
